from django.db import models

# Create your models here.
class User(models.Model):
    ON=1
    OFF=2
    STATUS_CHOICES={
        (ON,'正常'),
        (OFF,'锁定')
    }
    class Meta:
        verbose_name_plural='用户列表'
    user_name=models.CharField(max_length=30,unique=True,verbose_name="用户名")
    pwd=models.CharField(max_length=200,verbose_name="密码")
    status=models.IntegerField(verbose_name="状态",choices=STATUS_CHOICES,default=ON)
    phone=models.CharField(max_length=30,verbose_name="手机号")
class Address(models.Model):
    class Meta:
        verbose_name_plural ="送货地址"
    user=models.ForeignKey('User',on_delete=models.CASCADE)
    user_name=models.CharField(max_length=20,unique=True)
    phone=models.CharField(max_length=30)
    address=models.CharField(max_length=200,null=False)
    is_default=models.IntegerField()
    def __str__(self):
        return self.address+"("+self.user_name+")收，联系电话："+self.phone
class Goods(models.Model):
    class Meta:
        verbose_name_plural="商品列表"
    good_name=models.CharField(max_length=30)
    imr_url=models.ImageField(upload_to='img')
    price=models.IntegerField()
    intro=models.TextField()
    type=models.ManyToManyField('Type',null=True)
    K="克"
    J="斤"
    G="个"
    UNIT_CHOICES={
        (K , "克"),
        (J , "斤"),
        (G , "个")
    }
    unit=models.CharField(choices=UNIT_CHOICES,default="克",max_length=10)
    objects=models.Manager()

    def __str__(self):
       return self.good_name

class Type(models.Model):
    class Meta:
        verbose_name_plural="商品分类"
    type_name=models.CharField(max_length=30,unique=True)
    def __str__(self):
        return self.type_name
class Cart(models.Model):
    class Meta:
        verbose_name_plural="购物车列表"
    num=models.IntegerField(verbose_name="购买数量")
    user=models.ForeignKey("User",on_delete=models.CASCADE)
    goods=models.ManyToManyField(Goods)
class Order(models.Model):
    WFK=1
    YFK=2
    YFH=3
    YSH=4
    ORDER_STATUS_CHOICEZS={
       (WFK ,"未付款"),
       (YFK ,"已付款"),
       (YFH, "已发货"),
       (YSH, "已收货")

    }
    HUO=1
    WX=2
    ZFB=3
    YIN=4
    PAY_STATUS_CHOICEZS = {
        (HUO ,"货到付款"),
        (WX,"微信支付"),
        (ZFB,"支付宝"),
        (YIN ,"银行卡支付")

    }
    class Meta:
        verbose_name_plural="订单列表"
    order_no=models.CharField(max_length=30,verbose_name="订单号",unique=True)
    status=models.IntegerField(choices=ORDER_STATUS_CHOICEZS,default=WFK,verbose_name="订单状态")
    time=models.DateTimeField(auto_now_add=True,verbose_name="下单时间")
    user_name=models.CharField(max_length=20,null=True,verbose_name="联系人")
    phon=models.DecimalField(max_digits=12,decimal_places=0,null=True,verbose_name="联系电话")
    address=models.CharField(max_length=200,verbose_name="联系地址",null=True)
    user=models.ForeignKey("User",on_delete=models.CASCADE,verbose_name="用户名",null=True)
    total_price=models.IntegerField(verbose_name="实付款",null=True)
    pay_status=models.IntegerField(choices=PAY_STATUS_CHOICEZS,default=HUO,verbose_name="付款方式")
class OrderDetial(models.Model):
    good_name=models.CharField(max_length=30)
    price=models.IntegerField()
    num=models.IntegerField()
    img_url=models.CharField(max_length=100)
    total_price=models.IntegerField()
    order=models.ForeignKey('Order',on_delete=models.CASCADE)